package org.water.common.bind.method;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.water.common.bind.annotation.CurrentUser;
/**
 * 当前用户
 * @author qzy
 *
 */
public class CurrentUserMethodArgumentResolver implements HandlerMethodArgumentResolver {

	public CurrentUserMethodArgumentResolver() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public Object resolveArgument(MethodParameter methodparameter, ModelAndViewContainer modelandviewcontainer,
			NativeWebRequest nativewebrequest, WebDataBinderFactory webdatabinderfactory) throws Exception {
		CurrentUser currentUserAnnotation=methodparameter.getParameterAnnotation(CurrentUser.class);
		Object obj=nativewebrequest.getAttribute(currentUserAnnotation.value(), NativeWebRequest.SCOPE_SESSION);
		return obj;
	}

	@Override
	public boolean supportsParameter(MethodParameter methodparameter) {
		boolean result=methodparameter.hasParameterAnnotation(CurrentUser.class);
		return result;
	}

}
